package com.example.hellocharts;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        LineChart linechart = findViewById(R.id.linechart);
        float[] f1 = {2.0f, 9.0f, 3.0f, 4.0f, 7.0f, 3.0f, 5.0f, 1.0f, 7.0f};
        float[] f2 = {3.0f, 8.0f, 6.0f, 5.0f, 1.0f, 9.0f, 7.0f, 5.0f, 5.0f};
        List<ILineDataSet> s = new ArrayList<>();
        LineDataSet damo1=getdataset(f1,"");
        s.add(damo1);
        //设置线条颜色
        damo1.setColor(Color.parseColor("#ff00ff"));
        //不显示圆点
        damo1.setDrawCircles(false);
        //设置线条宽度
        damo1.setLineWidth(1.5f);
        //设置线条平滑
        damo1.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        XAxis xAxis=linechart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawAxisLine(true);
        // 标签倾斜
        xAxis.setLabelRotationAngle(45);

        LineDataSet damo2=getdataset(f2,"");
        s.add(damo2);
        damo2.setColor(Color.parseColor("#00ffff"));
        damo2.setLineWidth(1.0f);
        damo2.setDrawCircles(false);
        damo2.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        //得到Y轴
        YAxis yAxis=linechart.getAxisLeft();
        YAxis rightYAxis=linechart.getAxisRight();
        
        //设置Y轴是否显示
        rightYAxis.setEnabled(false);//右侧Y轴不显示
        //不显示网格线
        yAxis.setDrawGridLines(false);
        //设置Y轴之间最小间隔
        yAxis.setGranularity(1);
        yAxis.setAxisMinimum(0f);


        //图例;得到Lengend
        Legend legend=linechart.getLegend();
        //隐藏Lengend
        // legend.setEnabled(false);
        //折线图点标记
//        MyMarkerView mv=new MyMarkerView(this);
//        linechart.setMarker(mv);
        //设置数据
//        linechart.setData(damo1);
        //图标刷新
        linechart.invalidate();



        linechart.setData(new LineData(s));
        linechart.invalidate();
    }
//得到一条线
    private LineDataSet getdataset(float []f, String titer){
        List<Entry> data = new ArrayList<>();
        for (int i = 0; i < f.length; i++){
            Entry entry = new Entry(i, f[i]);
            data.add(entry);
        }
        return new LineDataSet(data, titer);
    }
}
